extends ../wechat

block content
  #container.container(style='padding: 15px; text-align: center;')
    h1 语音识别找电影
    .weui-btn.weui-btn_primary 点击开始录音
    p#title
    div#director
    div#year
    div#poster
  script.
    wx.config({
      debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: '#{appId}', // 必填，公众号的唯一标识
      timestamp: '#{timestamp}', // 必填，生成签名的时间戳
      nonceStr: '#{noncestr}', // 必填，生成签名的随机串
      signature: '#{signature}',// 必填，签名，见附录1
      jsApiList: [
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'startRecord',
        'stopRecord',
        'onVoiceRecordEnd',
        'translateVoice'
      ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    })

    console.log(window.location.href.split('#')[0])

    wx.ready(function (){
      wx.checkJsApi({
        jsApiList: ['onMenuShareTimeline', 'startRecord', 'onVoiceRecordEnd'],
        success: function(res) {
          console.log(res)
        }
      })

      var shareContent = {
        title: 'sousoussou',
        desc: '我搜出来了啥',
        link: 'https://wechat7online.iblack7.com',
        imgUrl: 'http://static.mukewang.com/static/img/common/logo.png',
        success: function () {
          window.alert('分享成功')
        },
        cancel: function () {
          window.alert('分享失败')
        }
      }

      wx.onMenuShareAppMessage(shareContent)
      var slides
      var isRecording = false

      $('#poster').on('click', function () {
        wx.previewImage(slides)
      })

      $('.weui-btn').on('click', function () {
        if (!isRecording) {
          isRecording = true
          $('.weui-btn').html('再点击停止录音解析搜索')
          wx.startRecord({
            success: function (res) {
              var localId = res.localId
              console.log(localId)
            },
            cancel: function () {
              window.alert('那就不能搜了哦')
            }
          })

          return
        }

        isRecording = false

        $('.weui-btn').html('点击开始录音')
        wx.stopRecord({
          success: function (res) {
            console.log('录制结束')
            var localId = res.localId

            wx.translateVoice({
              localId: localId,
              isShowProgressTips: 1,
              success: function (res) {
                var result = res.translateResult
                console.log('识别结果')
                $.ajax({
                  type: 'get',
                  url: 'https://api.douban.com/v2/movie/search?q=' + result,
                  dataType: 'jsonp',
                  jsonp: 'callback',
                  success: function(data) {
                    console.log(data)
                    var subject = data.subjects[0]
                    console.log(subject)

                    $('#title').html(subject.title)
                    $('#year').html(subject.year)
                    $('#director').html(subject.directors[0].name)
                    $('#poster').html('<img src="' + subject.images.large + '" />')

                    shareContent = {
                      title: subject.title,
                      desc: '我搜出来了 ' + subject.title,
                      link: 'https://github.com',
                      imgUrl: subject.images.large,
                      success: function () {
                        window.alert('分享成功')
                      },
                      cancel: function () {
                        window.alert('分享失败')
                      }
                    }

                    slides = {
                      current: subject.images.large,
                      urls: [subject.images.large],
                    }

                    data.subjects.forEach(function(item) {
                      slides.urls.push(item.images.large)
                    })

                    wx.onMenuShareAppMessage(shareContent)
                  }
                })
              }
            })
          }
        })
      })
    })